var $;
layui.config({
    base: "/static/layui/module/eleTree/"
}).use(['element','form','eleTree','layer','jquery','table','laydate'],function(){
	var element = layui.element;
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        $ = layui.jquery,
        table=layui.table,
        eleTree = layui.eleTree,
        laydate=layui.laydate;
    
    var roleId = undefined;
    
    table.render({
        elem:'#roleTable'
        ,title:'角色列表'
    	,url:'/permission/roleList'
	    ,method:'post'
        ,page: true //开启分页
	    ,limit: 10
	    ,limits: [10,15,18,20,30,40,50]
	    ,loading: true
        ,cols: [[ //表头
        	{type:'radio'}
        	,{field: 'id',hide: true, title: 'id'}
        	,{field: 'roleCode', title: '角色代码'}
            ,{field: 'roleName', title: '角色名称'}
        ]]
    	,text: {
    		none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
    	}
    	,toolbar:'#tableToolbar'
        ,defaultToolbar:[]
    });
    
    $("#search").click(function(){
    	var roleCode = $("#roleCode").val();
    	table.reload('roleTable', {
    		where: {
    			searchCode:roleCode
    		}
	    	,page: {
	    	    curr: 1
	    	}
		});
    });
    
    var treeParams = {
		elem: '.permissionTree',
        //data: data,
        url: "/permission/menuList",
        method: "post",
        contextmenuList: ["add","edit","remove"],
        renderAfterExpand: true,
        expandOnClickNode: false,
        showCheckbox: true,
        defaultExpandAll: true,
        highlightCurrent: true,
        contentType: 'application/x-www-form-urlencoded'
    }
    
    var el = eleTree.render(treeParams);
    
    $(".sumbitPermission").click(function(){
    	if(roleId == undefined){
    		layer.msg("请先选择赋予权限的角色！");
    		return;
    	}
    	top.layer.confirm('确认给该角色赋予勾选权限?', {icon: 3, title:'提示'}, function(index){
    		var shadeIndex = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.4});
    		var ids = new Array();
    		$.each(el.getChecked(false, true),function(index,e){
    			ids.push(e.id);
    		}); 
	    	$.post('/permission/comfire_permission',{roleId:roleId,permissionIds:JSON.stringify(ids)},function(d){
	    		if(d.code == '200'){
					top.layer.msg("赋权成功",{icon:1});
					treeParams.where = {roleId:roleId};
					el = eleTree.render(treeParams);
				}else{
					top.layer.msg("赋权失败,"+d.msg,{icon:2});
				}
    			top.layer.close(shadeIndex);
	    	},'json');
    	});
    });
    
    //单击行选中radio及加载permission信息
	table.on('row(roleTable)', function(obj){
		obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');//选中行样式
        obj.tr.find('input[lay-type="layTableRadio"]').prop("checked",true);
        form.render('radio');
        treeParams.where = {roleId:obj.data.id};
        roleId = obj.data.id;
        el = eleTree.render(treeParams);
	});
	
	table.on('toolbar(roleTable)', function(obj){
    	var tableCheckData = table.checkStatus(obj.config.id).data;
		switch(obj.event){
		    case 'add':
		    	var index = layer.open({
		    		type: 2
		    		,title: "新增"
		    		,maxmin: true
		    	  	,area: ['30%', '50%']
		    	  	,content: 'permission/addHtml'
		    	  	,btn: ['保存','返回']
		        	,yes: function(index, layero){
		        		var body = layer.getChildFrame('body', index);
		        		
		        		var roleCode = body.find("#roleCode").val();
		        		var roleName = body.find("#roleName").val();
		        		
		        		if(roleCode == ''){
		        			top.layer.msg("角色编码不能为空");
		        			return;
		        		}
		        		if(roleName == ''){
		        			top.layer.msg("角色名称不能为空");
		        			return;
		        		}
		        		
		        		var addData = {
	        				roleCode:roleCode,
	        				roleName:roleName
		        		}
		        		var shadeIndex = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.4});
		        		$.post('/permission/addRole',addData,function(d){
		        			if(d.code == '200'){
								top.layer.msg("新增成功",{icon:1});
								layer.close(index);
							}else{
								top.layer.msg("新增失败,"+d.msg,{icon:2});
							}
		        			top.layer.close(shadeIndex);
							table.reload('roleTable');
		        		},'json');
		    		}
		    	});
		    break;
		    case 'edit':
		    	if(roleId == undefined){
		    		layer.msg("请先选择修改数据！");
		    		return;
		    	}
		    	var index = layer.open({
		    		type: 2
		    		,title: "修改"
		    		,maxmin: true
		    	  	,area: ['30%', '50%']
		    	  	,content: 'permission/editHtml?id='+roleId
		    	  	,btn: ['保存','返回']
		        	,yes: function(index, layero){
		        		var body = layer.getChildFrame('body', index);
		        		
		        		var roleCode = body.find("#roleCode").val();
		        		var roleName = body.find("#roleName").val();
		        		
		        		if(roleCode == ''){
		        			top.layer.msg("角色编码不能为空");
		        			return;
		        		}
		        		if(roleName == ''){
		        			top.layer.msg("角色名称不能为空");
		        			return;
		        		}
		        		
		        		var addData = {
	        				id:roleId,
	        				roleCode:roleCode,
	        				roleName:roleName
		        		}
		        		var shadeIndex = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.4});
		        		$.post('/permission/editRole',addData,function(d){
		        			if(d.code == '200'){
								top.layer.msg("修改成功",{icon:1});
								layer.close(index);
								roleId = undefined;
							}else{
								top.layer.msg("修改失败,"+d.msg,{icon:2});
							}
		        			top.layer.close(shadeIndex);
							table.reload('roleTable');
		        		},'json');
		    		}
		    	});
		    break;
		    case 'del':
		    	if(roleId == undefined){
		    		layer.msg("请先选择删除数据！");
		    		return;
		    	}
		    	top.layer.confirm('是否删除?', {icon: 3, title:'提示'}, function(index){
		    		$.post('/permission/deleteRole',{id:roleId},function(d){
	        			if(d.code == '200'){
							top.layer.msg("删除成功",{icon:1});
							layer.close(index);
							roleId = undefined;
						}else{
							top.layer.msg("删除失败失败,"+d.msg,{icon:2});
						}
						table.reload('roleTable');
	        		},'json');
		    	});
		    break;
		}
	});
})
function loadTree(data){
	var setting={
	    check: {
	        enable: true,
	        chkStyle: "checkbox"//显示 checkbox 选择框，默认checkbox可选择值radio
	    }
		,data: {
			simpleData: {
				enable: true
			}
		}
		,callback:{
            onCheck:zTreeOnCheck
        }
	};
	var zTreeNodes=data;
	var city = $.fn.zTree.init($("#treeDemo"), setting, zTreeNodes);    
}
function zTreeOnCheck(event, treeId, treeNode) {
    alert("id="+treeNode.id+",name="+treeNode.name+",pid="+treeNode.pId);//弹出城市名字
}